# -*- coding: utf-8 -*-

from django.contrib import admin

from django_service.models import TableModel


class TableModelAdmin(admin.ModelAdmin):
    list_display = ['char_field', 'date_field', 'integer_field', 'float_field']
    list_display_links = ('char_field',)
    list_editable = ['integer_field', 'float_field', ]

    search_fields = ['char_field', ]
    list_filter = ['date_field', ]
    ordering = ['float_field', ]

    list_per_page = 5
    list_max_show_all = 200

    pass


def get_admin_class(table_model, search_fields=None, list_filter=None):
    class AdminClass(admin.ModelAdmin):
        pass

    AdminClass.list_per_page = 10
    AdminClass.list_max_show_all = 100
    AdminClass.show_full_result_count = True

    AdminClass.list_display = [field.name for field in table_model._meta.fields]

    if search_fields:
        AdminClass.search_fields = search_fields

    if list_filter:
        AdminClass.list_filter = list_filter

    return AdminClass


model_tuple = (TableModel,)
for table_model in model_tuple:
    admin.site.register(table_model, admin_class=get_admin_class(table_model))
